<template>
    <Tree ref="tree" :data="treeData" show-checkbox></Tree>
</template>
<script>
import { getResourceTree, saveRoleResource } from '@/api/sys/sys.rbac.service'
export default {
  props: {
    params: {
      type: Object,
      default () {
        return {
        }
      }
    }
  },
  data () {
    return {
      treeData: []
    }
  },
  mounted () {
    getResourceTree({
      roleId: this.params.id
    }).then(res => {
      if (res.code === 0) {
        this.treeData = res.data.data
      }
    })
  },
  methods: {
    submit () {
      return new Promise((resolve, reject) => {
        var ids = this.$refs.tree.getCheckedNodes().map(item => {
          return item.id
        })
        saveRoleResource({
          roleId: this.params.id,
          resourceIds: ids
        }).then(res => {
          resolve(res)
        }).catch(e => {
          reject(e)
        })
      })
    }
  }
}
</script>
